#ifndef YCCORELOGINUSER_H
#define YCCORELOGINUSER_H
#include <QJsonObject>
#include <QString>

namespace yc {

class LoginUser
{
public:
    LoginUser() : id_(0), auto_login_(false) { }
    explicit LoginUser(const QString& n, bool auto_login = false)
        : id_(0), name_(n), auto_login_(auto_login)
    {
    }
    LoginUser(int i, const QString& n, const QString& a, const QString& r, const QString& d)
        : id_(i), name_(n), access_token_(a), refresh_token_(r), device_id_(d), auto_login_(false)
    {
    }

    int id() const { return id_; }
    void setId(int id) { id_ = id; }
    const QString& name() const { return name_; }
    void setName(const QString& name) { name_ = name; }
    const QString& accessToken() const { return access_token_; }
    const QDateTime& accessTokenExpire() const { return access_token_expire_; }
    const QString& refreshToken() const { return refresh_token_; }
    const QDateTime& refreshTokenExpire() const { return refresh_token_expire_; }
    const QString& deviceId() const { return device_id_; }
    void setDeviceId(const QString& id) { device_id_ = id; }
    bool isValid() const { return !name_.isEmpty() && !access_token_.isEmpty(); }
    bool autoLogin() const { return auto_login_; }
    void setAutoLogin(bool auto_login) { auto_login_ = auto_login; }

    QJsonObject toJsonObject() const
    {
        QJsonObject jo;
        jo["id"] = id_;
        jo["name"] = name_;
        jo["accessToken"] = access_token_;
        jo["accessTokenExpire"] = access_token_expire_.toString(Qt::ISODate);
        jo["refreshToken"] = refresh_token_;
        jo["refreshTokenExpire"] = refresh_token_expire_.toString(Qt::ISODate);
        jo["deviceId"] = device_id_;
        jo["autoLogin"] = auto_login_;
        return jo;
    }
    void updateData(const QJsonObject& jo)
    {
        if (jo.contains("id")) {
            id_ = jo["id"].toInt(id_);
        }
        if (jo.contains("name")) {
            name_ = jo["name"].toString(name_);
        }
        if (jo.contains("accessToken")) {
            access_token_ = jo["accessToken"].toString(access_token_);
        }
        if (jo.contains("accessTokenExpire")) {
            access_token_expire_ =
                    QDateTime::fromString(jo["accessTokenExpire"].toString(), Qt::ISODate);
        }
        if (jo.contains("refreshToken")) {
            refresh_token_ = jo["refreshToken"].toString(refresh_token_);
        }
        if (jo.contains("refreshTokenExpire")) {
            refresh_token_expire_ =
                    QDateTime::fromString(jo["refreshTokenExpire"].toString(), Qt::ISODate);
        }
        if (jo.contains("deviceId")) {
            device_id_ = jo["deviceId"].toString(device_id_);
        }
        if (jo.contains("autoLogin")) {
            auto_login_ = jo["autoLogin"].toBool(auto_login_);
        }
    }

private:
    int id_;
    QString name_;
    QString access_token_;
    QDateTime access_token_expire_;
    QString refresh_token_;
    QDateTime refresh_token_expire_;
    QString device_id_;
    bool auto_login_;
};
} // namespace yc

#endif // YCCORELOGINUSER_H
